<?php

class Pm_model extends Model{
	
	function getPM($user_id){
		$this->db->join('users', 'users_id = pm_verzender');
		$this->db->where('pm_ontvanger', $user_id);
		$this->db->order_by('pm_tijd', 'DESC');
		$query = $this->db->get('pm');
		
		return $query;
	}
	
	function getPMInfo($pminfo){
		$this->db->join('users', 'users_id = pm_verzender');
		$this->db->where($pminfo);
		$query = $this->db->get('pm');
		
		return $query;
	}
	
	function verstuurPM($pminfo){
		$this->db->insert('pm', $pminfo);
	}
	
	function ontvangerBestaat($ontvangernaam){
		$this->db->where('users_naam', $ontvangernaam);
		$query = $this->db->get('users');
		
		return $query->num_rows();
	}
	
	function bestaatPM($pm_id){
		$this->db->where('pm_id',$pm_id);
		$query = $this->db->get('pm');
		
		return $query->num_rows();
	}
	
	function setGelezen($pm_id){
		$data = array(
				'pm_gelezen' => 1
		);
		
		$this->db->where('pm_id',$pm_id);
		$this->db->update('pm',$data);
	}
	
	function verwijderPM($pm_id){
		$this->db->where('pm_id',$pm_id);
		$this->db->delete('pm');
	}
	
	function zoekID($naam){
		$this->db->where('users_naam',$naam);
		$query = $this->db->get('users');
		
		foreach($query->result() as $item){
			$user_id = $item->users_id;
		}
			return $user_id;
	}
	
	function getPMVerzonden($user_id){
		$this->db->join('users', 'users_id = pm_ontvanger');
		$this->db->where('pm_verzender', $user_id);
		$this->db->order_by('pm_tijd', 'DESC');
		$query = $this->db->get('pm');
		
		return $query;
	}
}

?>